/********************************************************************
FileName    :   Dal_rktz.cs
CreateTime  :   2015-10-15 10:10
BaseClass   :   Dal_rktz
FileType    :   .cs
Author      :  minjie
Description :   Dal_rktzDAL层（简单三层）
*********************************************************************/

using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Data;
namespace OS.DAL
{
	/// <summary>
	/// Dal_rktz的摘要说明。
	/// </summary>
	public partial class Dal_rktz 
  	{ 
     		      
  		public bool Exists(string pzh) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) from rktz"); 
  			strSql.Append(" where "); 
           SqlParameter[] parameters = { new SqlParameter("@pzh", SqlDbType.Int,4)};  
  			return DbHelperSQL.Exists(strSql.ToString(),parameters); 
  		} 
  		 
  				 
  		 
  		/// <summary> 
  		/// 增加一条数据 
  		/// </summary> 
  		public  bool  Add(rktz_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into rktz(");			 
  	strSql.Append("rq,bmh,pzh,spbh,sl,jj,czy,mem,rkbm,gj,gjje,gysbh,cgd_pzh,bj,csbj,yfk,xh,kw) values ("); 
  	strSql.Append("	@rq,@bmh,@pzh,@spbh,@sl,@jj,@czy,@mem,@rkbm,@gj,@gjje,@gysbh,@cgd_pzh,@bj,@csbj,@yfk,@xh,@kw	 ) "); 
  			SqlParameter[] parameters = { 
              new SqlParameter("@rq", SqlDbType.DateTime) ,              
              new SqlParameter("@bmh", SqlDbType.Char,10) ,              
              new SqlParameter("@pzh", SqlDbType.Char,10) ,              
              new SqlParameter("@spbh", SqlDbType.Char,10) ,              
              new SqlParameter("@sl", SqlDbType.Decimal,9) ,              
              new SqlParameter("@jj", SqlDbType.Decimal,9) ,              
              new SqlParameter("@czy", SqlDbType.Char,10) ,              
              new SqlParameter("@mem", SqlDbType.Char,10) ,              
              new SqlParameter("@rkbm", SqlDbType.Char,10) ,              
              new SqlParameter("@gj", SqlDbType.Decimal,9) ,              
              new SqlParameter("@gjje", SqlDbType.Decimal,9) ,              
              new SqlParameter("@gysbh", SqlDbType.Char,10) ,              
              new SqlParameter("@cgd_pzh", SqlDbType.Char,10) ,              
              new SqlParameter("@bj", SqlDbType.Int,4) ,              
              new SqlParameter("@csbj", SqlDbType.Int,4) ,              
              new SqlParameter("@yfk", SqlDbType.Decimal,9) ,              
              new SqlParameter("@xh", SqlDbType.Int,4) ,              
              new SqlParameter("@kw", SqlDbType.Char,10)};             
           if (model.rq != null)  
              parameters[0].Value = model.rq;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.bmh != null)  
              parameters[1].Value = model.bmh;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.pzh != null)  
              parameters[2].Value = model.pzh;  
           else  
              parameters[2].Value = DBNull.Value;  
              
           if (model.spbh != null)  
              parameters[3].Value = model.spbh;  
           else  
              parameters[3].Value = DBNull.Value;  
              
           if (model.sl != null)  
              parameters[4].Value = model.sl;  
           else  
              parameters[4].Value = DBNull.Value;  
              
           if (model.jj != null)  
              parameters[5].Value = model.jj;  
           else  
              parameters[5].Value = DBNull.Value;  
              
           if (model.czy != null)  
              parameters[6].Value = model.czy;  
           else  
              parameters[6].Value = DBNull.Value;  
              
           if (model.mem != null)  
              parameters[7].Value = model.mem;  
           else  
              parameters[7].Value = DBNull.Value;  
              
           if (model.rkbm != null)  
              parameters[8].Value = model.rkbm;  
           else  
              parameters[8].Value = DBNull.Value;  
              
           if (model.gj != null)  
              parameters[9].Value = model.gj;  
           else  
              parameters[9].Value = DBNull.Value;  
              
           if (model.gjje != null)  
              parameters[10].Value = model.gjje;  
           else  
              parameters[10].Value = DBNull.Value;  
              
           if (model.gysbh != null)  
              parameters[11].Value = model.gysbh;  
           else  
              parameters[11].Value = DBNull.Value;  
              
           if (model.cgd_pzh != null)  
              parameters[12].Value = model.cgd_pzh;  
           else  
              parameters[12].Value = DBNull.Value;  
              
           if (model.bj != null)  
              parameters[13].Value = model.bj;  
           else  
              parameters[13].Value = DBNull.Value;  
              
           if (model.csbj != null)  
              parameters[14].Value = model.csbj;  
           else  
              parameters[14].Value = DBNull.Value;  
              
           if (model.yfk != null)  
              parameters[15].Value = model.yfk;  
           else  
              parameters[15].Value = DBNull.Value;  
              
           if (model.xh != null)  
              parameters[16].Value = model.xh;  
           else  
              parameters[16].Value = DBNull.Value;  
              
           if (model.kw != null)  
              parameters[17].Value = model.kw;  
           else  
              parameters[17].Value = DBNull.Value;  
              
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);			 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
              return false;; 
           }  
  		} 
  		 
  		/// <summary> 
  		/// 增加一条数据(字符串) 
  		/// </summary> 
  		public  string  AddStr(rktz_Entity model) 
  		{ 
  				if(model.bmh==null) 
  				{ 
  					model.bmh="";  
  				} 
  				if(model.pzh==null) 
  				{ 
  					model.pzh="";  
  				} 
  				if(model.spbh==null) 
  				{ 
  					model.spbh="";  
  				} 
  				if(model.sl==null) 
  				{ 
  					model.sl=0; 
  				} 
  				if(model.jj==null) 
  				{ 
  					model.jj=0; 
  				} 
  				if(model.czy==null) 
  				{ 
  					model.czy="";  
  				} 
  				if(model.mem==null) 
  				{ 
  					model.mem="";  
  				} 
  				if(model.rkbm==null) 
  				{ 
  					model.rkbm="";  
  				} 
  				if(model.gj==null) 
  				{ 
  					model.gj=0; 
  				} 
  				if(model.gjje==null) 
  				{ 
  					model.gjje=0; 
  				} 
  				if(model.gysbh==null) 
  				{ 
  					model.gysbh="";  
  				} 
  				if(model.cgd_pzh==null) 
  				{ 
  					model.cgd_pzh="";  
  				} 
  				if(model.bj==null) 
  				{ 
  					model.bj=0; 
  				} 
  				if(model.csbj==null) 
  				{ 
  					model.csbj=0; 
  				} 
  				if(model.yfk==null) 
  				{ 
  					model.yfk=0; 
  				} 
  				if(model.xh==null) 
  				{ 
  					model.xh=0; 
  				} 
  				if(model.kw==null) 
  				{ 
  					model.kw="";  
  				} 
  		 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into rktz(");			 
  	strSql.Append("rq,bmh,pzh,spbh,sl,jj,czy,mem,rkbm,gj,gjje,gysbh,cgd_pzh,bj,csbj,yfk,xh,kw) values ("); 
  	strSql.Append("	'"+ model.rq+"','"+ model.bmh+"','"+ model.pzh+"','"+ model.spbh+"',"+ model.sl+","+ model.jj+",'"+ model.czy+"','"+ model.mem+"','"+ model.rkbm+"',"+ model.gj+","+ model.gjje+",'"+ model.gysbh+"','"+ model.cgd_pzh+"',"+ model.bj+","+ model.csbj+","+ model.yfk+","+ model.xh+",'" +model.kw+"'	 ) "); 
           return strSql.ToString(); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 更新一条数据 
  		/// </summary> 
  		public bool Update(rktz_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("update rktz set "); 
              strSql.Append(" rq = @rq,"); 
              strSql.Append(" bmh = @bmh,"); 
              strSql.Append(" pzh = @pzh,"); 
              strSql.Append(" spbh = @spbh,"); 
              strSql.Append(" sl = @sl,"); 
              strSql.Append(" jj = @jj,"); 
              strSql.Append(" czy = @czy,"); 
              strSql.Append(" mem = @mem,"); 
              strSql.Append(" rkbm = @rkbm,"); 
              strSql.Append(" gj = @gj,"); 
              strSql.Append(" gjje = @gjje,"); 
              strSql.Append(" gysbh = @gysbh,"); 
              strSql.Append(" cgd_pzh = @cgd_pzh,"); 
              strSql.Append(" bj = @bj,"); 
              strSql.Append(" csbj = @csbj,"); 
              strSql.Append(" yfk = @yfk,"); 
              strSql.Append(" xh = @xh,"); 
              strSql.Append(" kw = @kw"); 
  			strSql.Append(" where  pzh=@pzh "); 
  						 
  SqlParameter[] parameters = { 
              new SqlParameter("@rq", SqlDbType.DateTime) ,              
              new SqlParameter("@bmh", SqlDbType.Char,10) ,              
              new SqlParameter("@pzh", SqlDbType.Char,10) ,              
              new SqlParameter("@spbh", SqlDbType.Char,10) ,              
              new SqlParameter("@sl", SqlDbType.Decimal,9) ,              
              new SqlParameter("@jj", SqlDbType.Decimal,9) ,              
              new SqlParameter("@czy", SqlDbType.Char,10) ,              
              new SqlParameter("@mem", SqlDbType.Char,10) ,              
              new SqlParameter("@rkbm", SqlDbType.Char,10) ,              
              new SqlParameter("@gj", SqlDbType.Decimal,9) ,              
              new SqlParameter("@gjje", SqlDbType.Decimal,9) ,              
              new SqlParameter("@gysbh", SqlDbType.Char,10) ,              
              new SqlParameter("@cgd_pzh", SqlDbType.Char,10) ,              
              new SqlParameter("@bj", SqlDbType.Int,4) ,              
              new SqlParameter("@csbj", SqlDbType.Int,4) ,              
              new SqlParameter("@yfk", SqlDbType.Decimal,9) ,              
              new SqlParameter("@xh", SqlDbType.Int,4) ,              
              new SqlParameter("@kw", SqlDbType.Char,10)};             
           if (model.rq != null)  
              parameters[0].Value = model.rq;  
           else  
              parameters[0].Value = DBNull.Value;  
              
           if (model.bmh != null)  
              parameters[1].Value = model.bmh;  
           else  
              parameters[1].Value = DBNull.Value;  
              
           if (model.pzh != null)  
              parameters[2].Value = model.pzh;  
           else  
              parameters[2].Value = DBNull.Value;  
              
           if (model.spbh != null)  
              parameters[3].Value = model.spbh;  
           else  
              parameters[3].Value = DBNull.Value;  
              
           if (model.sl != null)  
              parameters[4].Value = model.sl;  
           else  
              parameters[4].Value = DBNull.Value;  
              
           if (model.jj != null)  
              parameters[5].Value = model.jj;  
           else  
              parameters[5].Value = DBNull.Value;  
              
           if (model.czy != null)  
              parameters[6].Value = model.czy;  
           else  
              parameters[6].Value = DBNull.Value;  
              
           if (model.mem != null)  
              parameters[7].Value = model.mem;  
           else  
              parameters[7].Value = DBNull.Value;  
              
           if (model.rkbm != null)  
              parameters[8].Value = model.rkbm;  
           else  
              parameters[8].Value = DBNull.Value;  
              
           if (model.gj != null)  
              parameters[9].Value = model.gj;  
           else  
              parameters[9].Value = DBNull.Value;  
              
           if (model.gjje != null)  
              parameters[10].Value = model.gjje;  
           else  
              parameters[10].Value = DBNull.Value;  
              
           if (model.gysbh != null)  
              parameters[11].Value = model.gysbh;  
           else  
              parameters[11].Value = DBNull.Value;  
              
           if (model.cgd_pzh != null)  
              parameters[12].Value = model.cgd_pzh;  
           else  
              parameters[12].Value = DBNull.Value;  
              
           if (model.bj != null)  
              parameters[13].Value = model.bj;  
           else  
              parameters[13].Value = DBNull.Value;  
              
           if (model.csbj != null)  
              parameters[14].Value = model.csbj;  
           else  
              parameters[14].Value = DBNull.Value;  
              
           if (model.yfk != null)  
              parameters[15].Value = model.yfk;  
           else  
              parameters[15].Value = DBNull.Value;  
              
           if (model.xh != null)  
              parameters[16].Value = model.xh;  
           else  
              parameters[16].Value = DBNull.Value;  
              
           if (model.kw != null)  
              parameters[17].Value = model.kw;  
           else  
              parameters[17].Value = DBNull.Value;  
              
              int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 删除一条数据 
  		/// </summary> 
  		public bool Delete(string  pzh) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from rktz "); 
  			strSql.Append(" where  pzh=@pzh"); 
  			SqlParameter[] parameters = {  new SqlParameter("@pzh", SqlDbType.Char,10) };  
    parameters[0].Value = pzh;  
   
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 批量删除一批数据 
  		/// </summary> 
  		public bool DeleteList(string pzhlist ) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from rktz "); 
  			strSql.Append(" where ID in ("+pzhlist + ")  "); 
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString()); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 得到一个对象实体 
  		/// </summary> 
  		public rktz_Entity GetModel(string   pzh) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select  rq,bmh,pzh,spbh,sl,jj,czy,mem,rkbm,gj,gjje,gysbh,cgd_pzh,bj,csbj,yfk,xh,kw");	
		strSql.Append("  from rktz"); 
  			strSql.Append(" where  pzh=@pzh "); 
  			SqlParameter[] parameters = {  new SqlParameter("@pzh", SqlDbType.Char,10) };  
    parameters[0].Value = pzh;  
  			 
  			rktz_Entity model=new rktz_Entity(); 
  			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); 
  			 
  			if(ds!=null&&ds.Tables[0].Rows.Count>0) 
  			{ 
  		DataTable dt=ds.Tables[0];	  
  				if(dt.Rows[0]["rq"]!=null&&dt.Rows[0]["rq"].ToString()!="") 
  				{ 
  					model.rq=DateTime.Parse(dt.Rows[0]["rq"].ToString()); 
  				} 
  				model.bmh= dt.Rows[0]["bmh"].ToString(); 
  				model.pzh= dt.Rows[0]["pzh"].ToString(); 
  				model.spbh= dt.Rows[0]["spbh"].ToString(); 
  				if(dt.Rows[0]["sl"]!=null&&dt.Rows[0]["sl"].ToString()!="") 
  				{ 
  					model.sl=decimal.Parse(dt.Rows[0]["sl"].ToString()); 
  				} 
  				if(dt.Rows[0]["jj"]!=null&&dt.Rows[0]["jj"].ToString()!="") 
  				{ 
  					model.jj=decimal.Parse(dt.Rows[0]["jj"].ToString()); 
  				} 
  				model.czy= dt.Rows[0]["czy"].ToString(); 
  				model.mem= dt.Rows[0]["mem"].ToString(); 
  				model.rkbm= dt.Rows[0]["rkbm"].ToString(); 
  				if(dt.Rows[0]["gj"]!=null&&dt.Rows[0]["gj"].ToString()!="") 
  				{ 
  					model.gj=decimal.Parse(dt.Rows[0]["gj"].ToString()); 
  				} 
  				if(dt.Rows[0]["gjje"]!=null&&dt.Rows[0]["gjje"].ToString()!="") 
  				{ 
  					model.gjje=decimal.Parse(dt.Rows[0]["gjje"].ToString()); 
  				} 
  				model.gysbh= dt.Rows[0]["gysbh"].ToString(); 
  				model.cgd_pzh= dt.Rows[0]["cgd_pzh"].ToString(); 
  				if(dt.Rows[0]["bj"]!=null&&dt.Rows[0]["bj"].ToString()!="") 
  				{ 
  					model.bj=int.Parse(dt.Rows[0]["bj"].ToString()); 
  				} 
  				if(dt.Rows[0]["csbj"]!=null&&dt.Rows[0]["csbj"].ToString()!="") 
  				{ 
  					model.csbj=int.Parse(dt.Rows[0]["csbj"].ToString()); 
  				} 
  				if(dt.Rows[0]["yfk"]!=null&&dt.Rows[0]["yfk"].ToString()!="") 
  				{ 
  					model.yfk=decimal.Parse(dt.Rows[0]["yfk"].ToString()); 
  				} 
  				if(dt.Rows[0]["xh"]!=null&&dt.Rows[0]["xh"].ToString()!="") 
  				{ 
  					model.xh=int.Parse(dt.Rows[0]["xh"].ToString()); 
  				} 
  				model.kw= dt.Rows[0]["kw"].ToString(); 
  				return model; 
  			} 
  			else 
  			{ 
  				return null; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 分页获取数据列表 
  		/// </summary> 
  		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  		 strSql.Append("SELECT * FROM ( "); 
  		 strSql.Append(" SELECT ROW_NUMBER() OVER ("); 
  if (!string.IsNullOrEmpty(orderby.Trim())) 
 { 
    strSql.Append("order by T." + orderby); 
 } 
else  
 { 
     strSql.Append("order by T.pzh desc"); 
 } 
		 strSql.Append(")AS Row, T.*  from rktz T "); 
  		 if (!string.IsNullOrEmpty(strWhere.Trim()))  
  			{ 
  			 strSql.Append(" WHERE " + strWhere); 
  			} 
  		    strSql.Append(" ) TT"); 
  	        strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 获得数据列表 
  		/// </summary> 
  		public DataSet GetList(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select * "); 
  			strSql.Append(" FROM rktz "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得前几行数据 
  		/// </summary> 
  		public DataSet GetList(int Top,string strWhere,string filedOrder) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select "); 
  			if(Top>0) 
  			{ 
  				strSql.Append(" top "+Top.ToString()); 
  			} 
  			strSql.Append(" * "); 
  			strSql.Append(" FROM rktz "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			strSql.Append(" order by " + filedOrder); 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得记录条数 
  		/// </summary> 
  		public int GetRecordCount(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) "); 
  			strSql.Append(" FROM rktz "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  		   object obj=	DbHelperSQL.GetSingle(strSql.ToString()); 
  			if(obj!=null && obj.ToString()!="") 
  			{ 
              	return Convert.ToInt32(obj); 
  			} 
  			else 
  			return 0; 
  		}
        public DataSet GetListByPage(int pageIndex, int pageSize, string strWhere)
        {
            int oldPage = (pageIndex - 1) * pageSize;

            string strWhere1 = "";
            string strWhere2 = "";
            if (strWhere != "")
            {
                strWhere1 = " where " + strWhere;
                strWhere2 = " and " + strWhere;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " *from rktz ");
            strSql.Append("where id not in (select top " + oldPage + " id from rktz " + strWhere1 + "  order by id)   " + strWhere2 + " order by id  ");
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int Update(List<rktz_Entity> ls)
        {
            int count = 0;
            if (ls.Count > 0)
            {
                List<string> sqlList = new List<string>();
                foreach (rktz_Entity entity in ls)
                {
                    string sql = "update rktz  set csbj=1 where xh= " + entity.xh + "  and pzh='" + entity.pzh + "' and spbh ='" + entity.spbh + "'";
                    sqlList.Add(sql);
                }


                if (sqlList.Count > 0)
                    count = DbHelperSQL.ExecuteSqlTran(sqlList);
            }
            return count;
        }
   
      
  	} 
  
}
